package com.yd.mvc.userController.user;

import com.yd.app.service.sys.UserService;
import com.yd.app.util.BusinessException;
import com.yd.app.util.LoginInfoException;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.BaseController;
import com.yd.mvc.baseController.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Controller
public class LoginController extends BaseController{

	// 远程调用 @Reference()、本地调用@Autowired

	@Autowired
	UserService userService;

	/**
	 *进入登录页面
	 */
/*	@RequestMapping(value="/",method = RequestMethod.GET)
	public String loginUI(HttpServletRequest request,HttpServletResponse response){
		try{
			boolean islogin= AuthUtil.auth(request,response);
			if(islogin){
				return "redirect:/vs/vs/lt";
			}else{
				return "redirect:/user/login/index";
			}
		}catch (Exception e){
			return "redirect:/user/login/index";
		}
	}*/
	//管理员登录
	@ResponseBody
	@RequestMapping(value = "/login/{username}/{password}", method = RequestMethod.GET)
	public String adminLogin(@PathVariable("username") String username, @PathVariable("password") String password, @RequestParam(value = "ref", required = true) String ref, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User u = userService.login(username.toLowerCase(), password);
		if (u == null)
			throw new BusinessException(500, "登录失败", "error_login");
		Cookie c = AuthUtil.login(response, u.getId(),u.getUserName(),u.getRealName(),u.getCompanyName(),getStringFromList(u.getPermCodeList()));
		return ref;
	}

	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response){
		AuthUtil.AuthedUser user = AuthUtil.getCurrentUser();
		Cookie cookie = new Cookie(AuthUtil.getCookieName(), String.format("0%s", UUID.randomUUID().toString()));
		cookie.setPath(AuthUtil.getCookiePath());
		//cookie.setDomain(AuthUtil.getCookieDomain());
		cookie.setMaxAge((int) AuthUtil.getExpierinterval()/1000);
		response.reset();
		response.addCookie(cookie);
		Date date = new Date();
		return "redirect:/";
	}
	@ResponseBody
	@RequestMapping(value = "api/user", method = RequestMethod.GET)
	public User getCurUserInfo(){
		AuthUtil.AuthedUser user= AuthUtil.getCurrentUser();
		User user_c=null;
		user_c=userService.getUserById(user.getUserId());
		user_c.setPassword("");
		return user_c;
	}
	/*@ResponseBody
	@RequestMapping(value = "/cu/detailed/{userId}", method = RequestMethod.GET)
	public User getDetailedCurUserInfo(@PathVariable("userId") String userId){
		AuthUtil.AuthedUser user= AuthUtil.getCurrentUser();
		User user_c=null;
		if(user.getRoles().equals("2")){//学生
			user_c=userService.getUserById(user.getUserId());
		}else{
			//user_c=userService.getManagerUserById(user.getUserId());
		}
		if(user!=null){
			user_c.setPassword("");
		}
		return user_c;
	}*/
	/**
	 * 登录验证
	 * @param password 密码
	 * @return
	 */
	@RequestMapping(value="/login/do",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> doLogin(String username, String password, HttpServletResponse response) throws Exception {
		final User user=userService.loginDo(username,password);
		if(user!=null){
			AuthUtil.login(response,user.getId(),user.getUserName(),user.getRealName(),user.getCompanyName(),getStringFromList(user.getPermCodeList()));
			return new HashMap<String,Object>(){{
				put("id",user.getId());
				put("name",user.getUserName());
			}};
		}else{
			throw new BusinessException(500,"用户名或密码错误！");
		}
	}
}
